package com.ltmonitor.web.controller.map;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ltmonitor.entity.GPSRealData;
import com.ltmonitor.entity.VehicleData;
import com.ltmonitor.service.IRealDataService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ltmonitor.service.MapFixService;
import com.ltmonitor.util.DateUtil;
import com.ltmonitor.util.StringUtil;
import com.ltmonitor.vo.PointLatLng;
import com.ltmonitor.web.controller.PaginateAction;

/**
 * 区域查询车辆
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/vehicleInArea")
public class VehicleInAreaAction extends PaginateAction {

	@Autowired
	private IRealDataService realDataService;
	@RequestMapping("/view")
	public String view(String strRoutePoints, Model m) {
		String vehicleInAreaData = "";
		if (strRoutePoints != null) {
			RectBound b = setRoutePoints(strRoutePoints);

			Map<String, Object> params = new HashMap<String, Object>();
			String queryId = "vehicle.selectGpsRealData";
			params.put("minLongitude", b.minLongitude);
			params.put("maxLongitude", b.maxLongitude);
			params.put("minLatitude", b.minLatitude);
			params.put("maxLatitude", b.maxLatitude);
			params.put("areaBounds", true);
			List<Map<String, Object>> ls = this.queryService.query(queryId,
					params);
			JSONArray jy = new JSONArray();
			for (Map<String, Object> rowData : ls) {
				super.convert(rowData, "plateColor", "PlateColor");
				Date sendTime = (Date) rowData.get("sendTime");
				if (sendTime != null) {
					rowData.put("sendTime", DateUtil.datetimeToString(sendTime));
				}
				if (rowData.get("latitude") != null) {
					double latitude = Double.parseDouble(""
							+ rowData.get("latitude"));
					double longitude = Double.parseDouble(""
							+ rowData.get("longitude"));
					PointLatLng pl = MapFixService.fix(latitude, longitude,
							super.getMapType());
					rowData.put("latitude", pl.getLat());
					rowData.put("longitude", pl.getLng());
				}

				String simNo = (String)rowData.get("simNo");
				GPSRealData rd = this.realDataService.get(simNo);
				if(rd != null) {
					rowData.put("online", rd.getOnline());
					rowData.put("sendTime", DateUtil.datetimeToString(rd.getSendTime()));
				}
				VehicleData vd = super.vehicleDataService.load(rd.getVehicleId());
				String vehicleIcon = this.getMapIcon(vd,rd);
				rowData.put("vehicleIcon", vehicleIcon);

				JSONObject jo = JSONObject.fromObject(rowData);
				jy.add(jo);
			}
			vehicleInAreaData = jy.toString();

			m.addAttribute("minLongitude", b.minLongitude);
			m.addAttribute("maxLongitude", b.maxLongitude);
			m.addAttribute("minLatitude", b.minLatitude);
			m.addAttribute("maxLatitude", b.maxLatitude);

		}
		m.addAttribute("vehicleInAreaData", vehicleInAreaData);
		return "/vehicleInArea/vehicleInArea";
	}

	// 定时定位车辆查询
	@RequestMapping("/viewVehicleInAreaAndTime")
	public String viewVehicleInAreaAndTime(String strRoutePoints,
			String areaType, @RequestParam(defaultValue = "0") double radius,
			Model m) {
		if (strRoutePoints != null) {
			strRoutePoints = getOriginRoutePoints(strRoutePoints);
		}

		String startTime1 = DateUtil.toStringByFormat(new Date(), "yyyyMMdd");
		String endTime1 = DateUtil.toStringByFormat(new Date(), "yyyyMMdd");

		// Date d1 = DateUtil.toDateByFormat(startTime1, "yyyyMMdd");
		// Date d2 = DateUtil.getDate(d1, Calendar.DAY_OF_YEAR, 1);
		// String limitTime = DateUtil.toStringByFormat(d2, "yyyyMMdd");
		// 只能查询相隔两天的记录
		// if (endTime1.compareTo(limitTime) > 0)
		// endTime1 = limitTime;
		String tableName1 = "gpsInfo" + startTime1;
		String tableName2 = "gpsInfo" + endTime1;

		m.addAttribute("strRoutePoints", strRoutePoints);
		m.addAttribute("tableName1", tableName1);
		m.addAttribute("tableName2", tableName2);

		String startTime = DateUtil.toStringByFormat(new Date(),
				"yyyy-MM-dd 00:00:00");
		String endTime = DateUtil.toStringByFormat(new Date(),
				"yyyy-MM-dd 23:59:59");
		m.addAttribute("startTime", startTime);
		m.addAttribute("endTime", endTime);

		m.addAttribute("radius", radius);
		m.addAttribute("areaType", areaType);
		return "/vehicleInArea/vehicleInAreaAndTime";
	}

	private String getOriginRoutePoints(String strRoutePoints) {
		String[] points = strRoutePoints.split(";");

		String mapType = super.getMapType();// 当前用户使用的地图类型
		String fixStrPoints = "";
		String firstPt = null;
		for (String strPt : points) {
			if (StringUtil.isNullOrEmpty(strPt))
				continue;
			String[] pt = strPt.split(",");
			Double lng = Double.parseDouble(pt[0]);
			Double lat = Double.parseDouble(pt[1]);

			PointLatLng p = MapFixService.reverse(lat, lng, mapType);
			fixStrPoints += p.lng + "," + p.lat + ";";
			if (firstPt == null)
				firstPt = p.lng + "," + p.lat;

		}

		if (firstPt.equals(points[points.length - 1])) {
			strRoutePoints = fixStrPoints.substring(0,
					fixStrPoints.lastIndexOf(';'));
		} else {
			strRoutePoints = fixStrPoints + firstPt;
		}
		return strRoutePoints;
	}

	private RectBound setRoutePoints(String strRoutePoints) {
		String[] points = strRoutePoints.split(";");
		int m = 1;
		double maxLatitude = 0;
		double maxLongitude = 0;
		double minLatitude = 1000;
		double minLongitude = 1000;

		String mapType = super.getMapType();// 当前用户使用的地图类型
		String fixStrPoints = "";
		for (String strPt : points) {
			if (StringUtil.isNullOrEmpty(strPt))
				continue;
			String[] pt = strPt.split(",");
			Double lng = Double.parseDouble(pt[0]);
			Double lat = Double.parseDouble(pt[1]);

			PointLatLng p = MapFixService.reverse(lat, lng, mapType);
			fixStrPoints += p.lng + "," + p.lat + ";";

			if (lat > maxLatitude)
				maxLatitude = lat;
			if (lat < minLatitude)
				minLatitude = lat;
			if (lng > maxLongitude)
				maxLongitude = lng;
			if (lng < minLongitude)
				minLongitude = lng;
		}
		strRoutePoints = fixStrPoints.substring(0,
				fixStrPoints.lastIndexOf(';'));
		// 将地图坐标还原成原始坐标，这样可以和GPS坐标进行比对，进行区域查车
		PointLatLng p1 = MapFixService.reverse(maxLatitude, maxLongitude,
				mapType);
		PointLatLng p2 = MapFixService.reverse(minLatitude, minLongitude,
				mapType);
		maxLatitude = p1.getLat();
		maxLongitude = p1.getLng();
		minLatitude = p2.getLat();
		minLongitude = p2.getLng();

		RectBound b = new RectBound();
		b.maxLatitude = maxLatitude;
		b.minLatitude = minLatitude;
		b.maxLongitude = maxLongitude;
		b.minLongitude = minLongitude;
		return b;
	}

	public class RectBound {
		public double maxLatitude;
		public double maxLongitude;
		public double minLatitude;
		public double minLongitude;
	}

}
